package com.ykdl.member.kid.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.ykdl.member.kid.models.ArticleBean;
import com.ykdl.member.kid.models.CatalogArticleItem;
import com.ykdl.member.kid.models.CatalogBean;
import com.ykdl.member.kid.models.CatalogItemBean;
import com.ykdl.member.kid.models.CatalogTopicItem;

import net.wxxr.http.interfaces.IJsonParser;

public class FreakyChatChildListBean implements Serializable, IJsonParser {
	private static final long serialVersionUID = -584559126385346897L;
	
	private ArrayList<CatalogBean>  catalogs = null;
	private int next_cursor = 0;
	private int previous_cursor = 0;
	private int total_number = 0;
	private boolean isExpired = false;
	public boolean isExpired() {
		return isExpired;
	}
	public void setExpired(boolean isExpired) {
		this.isExpired = isExpired;
	}
	public ArrayList<CatalogBean> getCatalogs() {
		if(catalogs == null){
			catalogs = new ArrayList<CatalogBean>();
		}
		return catalogs;
	}
	public void setCatalogs(ArrayList<CatalogBean> catalogs) {
		this.catalogs = catalogs;
	}
	public int getNext_cursor() {
		return next_cursor;
	}
	public void setNext_cursor(int next_cursor) {
		this.next_cursor = next_cursor;
	}
	public int getPrevious_cursor() {
		return previous_cursor;
	}
	public void setPrevious_cursor(int previous_cursor) {
		this.previous_cursor = previous_cursor;
	}


	public int getTotal_number() {
		return total_number;
	}


	public void setTotal_number(int total_number) {
		this.total_number = total_number;
	}


	@Override
	public IJsonParser parse(String json) throws Exception {
		Gson  gson = null;
		final FreakyChatChildListBean  bean = new FreakyChatChildListBean();
		if(null != json){
			final GsonBuilder b = new GsonBuilder();
			gson  = b.create();
			if("null".equals(json))return bean;
			if("{}".equals(json) || "[]".equals(json))return bean;
			final JSONObject  object = new JSONObject(json);
			if(json.startsWith("{") && json.endsWith("}")
					&& object.has("error")
					&& object.getInt("error") == 20007){
				bean.setExpired(true);
				return bean;
			}
			
			final String  list = object.getString("list");
			if("null".equals(list) || list.equals("[]")) return bean;
			
			final JSONArray  doctor_talks_json_array = object.getJSONArray("list");
			if(doctor_talks_json_array != null && doctor_talks_json_array.length() > 0){
				for(int i = 0 ; i < doctor_talks_json_array.length() ; i++){
					final String doctor_talk_item = doctor_talks_json_array.getString(i);
					if(doctor_talk_item == null || doctor_talk_item.equals("{}")) continue;
					JSONObject  jo = new JSONObject(doctor_talk_item);
					if(jo.has("catalog_id") && !jo.isNull("catalog_id")){
						//int catalog_type = jo.getInt("catalog_type");
						final String catalog_items = jo.getString("items");
						//if(catalog_items == null || catalog_items.equals("[]")) continue;
						List<CatalogItemBean>  item = null;
						//if(catalog_type == 1){ // 话题(只解析话题) 或 问题
							item = gson.fromJson(catalog_items, new TypeToken<List<CatalogTopicItem>>(){}.getType());
							//if(null == ((CatalogTopicItem)item.get(0)).getItem()) continue;
					//	}
//						else if(jo.getInt("catalog_type") == 2){ //文章
//							item = gson.fromJson(catalog_items, new TypeToken<List<CatalogArticleItem>>(){}.getType());
//							if(null == ((CatalogArticleItem)item.get(0)).getItem()) continue;
//						}
						
						CatalogBean  catalog = gson.fromJson(doctor_talk_item, CatalogBean.class);
						
						if(item != null){
							catalog.setCatalogItems(item);
						}
						bean.getCatalogs().add(catalog);
					}
				}
			}
			bean.setPrevious_cursor(object.getInt("previous_cursor"));
			bean.setNext_cursor(object.getInt("next_cursor"));
			bean.setTotal_number(object.getInt("total_number"));
			
			return bean;
		}
		return bean;
	}

}
